package com.softroad.help.app.login;

import java.util.HashMap;
import java.util.Map;

import net.arnx.jsonic.JSON;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;

import com.softroad.help.app.AppManager;
import com.softroad.help.app.R;
import com.softroad.help.app.bean.LoginResponse;
import com.softroad.help.app.bean.UserBean;
import com.softroad.help.app.constant.Apis;
import com.softroad.help.app.handler.StringHandler;
import com.softroad.help.app.tools.HttpTool;
import com.softroad.help.app.tools.StringTool;
import com.softroad.help.app.tools.ToastTool;
import com.softroad.help.app.ui.BaseActivity;
import com.softroad.help.app.ui.MainActivity;

@SuppressLint("NewApi")
public class LoginActivity extends BaseActivity implements OnClickListener,
		OnTouchListener {

	private Button loginBtn;
	private EditText nameEditText;
	private EditText passEditText;

	public static void startActivity(Context context) {
		context.startActivity(new Intent(context, LoginActivity.class));
	}

	@Override
	public int bindLayout() {
		return R.layout.activity_login;
	}

	@Override
	public void initView(View view) {
		action_back.setVisibility(View.GONE);
		action_right.setText(R.string.register);
		action_title.setText(R.string.login_title_content);
		nameEditText = $(R.id.name);
		passEditText = $(R.id.pass);
		loginBtn = $(R.id.login_btn);
	}

	@Override
	public void initData(Context mContext) {
	}

	@Override
	public void initListener() {
		nameEditText.setOnTouchListener(this);
		passEditText.setOnTouchListener(this);
		action_back.setOnClickListener(this);
		loginBtn.setOnClickListener(this);
		action_right.setOnClickListener(this);
	}

	@Override
	public boolean onTouch(View view, MotionEvent arg1) {
		if (view.getId() == R.id.name) {
			nameEditText.setBackground(getResources().getDrawable(
					R.drawable.edit_down));
			passEditText.setBackground(getResources().getDrawable(
					R.drawable.edit_up));
		} else if (view.getId() == R.id.pass) {
			passEditText.setBackground(getResources().getDrawable(
					R.drawable.edit_down));
			nameEditText.setBackground(getResources().getDrawable(
					R.drawable.edit_up));
		}
		return false;
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.action_back:
			AppManager.getAppManager().finishActivity();
			break;

		case R.id.action_right:
			RegisterActivity.startActivity(getContext());
			break;

		case R.id.login_btn:
			String name = nameEditText.getText().toString().trim();
			String pass = passEditText.getText().toString().trim();
			if (!StringTool.isNoBlankAndNoNull(name)) {
				ToastTool.toastMessage(getContext(), R.string.phone_lack);
				return;
			}
			if (!StringTool.isNoBlankAndNoNull(pass)) {
				ToastTool.toastMessage(getContext(), R.string.author_code_lack);
				return;
			}
			loginBy(name, pass);
			break;
		default:
			break;
		}
	}

	private void loginBy(String name, String pass) {
		Map maps = new HashMap<String, String>();
		maps.put("userName", name);
		maps.put("passWrd", pass);
		HttpTool.get(getContext(), Apis.HOST + Apis.LOGIN, maps,
				new StringHandler(loading) {

					@Override
					public void success(String response) {
						if (StringTool.isNoBlankAndNoNull(response)) {
							LoginResponse bean = JSON.decode(response,
									LoginResponse.class);
							if (bean != null) {
								String status = bean.getStatus();
								if (StringTool.isNoBlankAndNoNull(status)
										&& "0".equalsIgnoreCase(status)) {
									ToastTool.toastMessage(getContext(),
											bean.getMessage());
									UserBean userBean = bean.getData();
									if (userBean != null) {
										mApplication.saveObject(userBean,
												"user");
										mApplication.setLogin(true);
										mApplication.setUserBean(userBean);
										MainActivity
												.startActivity(getContext());
										AppManager.getAppManager()
												.finishActivity();
									}
								} else {
									ToastTool.toastMessage(getContext(),
											bean.getMessage());
								}
							}
						}
						loading.setVisibility(View.GONE);
						loginBtn.setEnabled(true);
					}

					@Override
					public void failure(int statusCode, String responseBody,
							Throwable e) {
						// TODO Auto-generated method stub
						loginBtn.setEnabled(true);
					}
				}, "utf-8");

	}

	@Override
	public void resume() {

	}

	@Override
	public void destroy() {

	}

}
